// 单聊会话控制
import { Request, Response, NextFunction } from 'express';
import * as conversationService from '../services/conversation.service';

export const listConversations = async (req: Request, res: Response, next: NextFunction) => {
  try {
    const uid = Number((req as any).user.id);
    const list = await conversationService.getConversations(uid);
    res.safeJson(list, '获取成功');
  } catch (err) {
    next(err);
  }
};

export const markAsRead = async (req: Request, res: Response, next: NextFunction) => {
  try {
    const uid = Number((req as any).user.id);
    const conversationId = Number(req.body.conversation_id);
    await conversationService.resetUnread(uid, conversationId);
    res.safeJson(null, '已设为已读');
  } catch (err) {
    next(err);
  }
};

export const removeConversation = async (req: Request, res: Response, next: NextFunction) => {
  try {
    const uid = Number((req as any).user.id);
    const id = Number(req.params.id);
    await conversationService.deleteConversation(uid, id);
    res.safeJson(null, '删除成功');
  } catch (err) {
    next(err);
  }
};
